package com.orangesoft.tescherfriend.business.user.dao.impl;

import com.orangesoft.common.entity.BaseDTO;
import com.orangesoft.tescherfriend.business.user.entity.User;
import com.orangesoft.tescherfriend.business.user.dto.UserDTO;
import com.orangesoft.tescherfriend.business.user.entity.User_;
import com.orangesoft.tescherfriend.business.user.dao.UserDaoCustom;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.lang.Boolean;
import java.lang.String;
import java.lang.String;
import java.lang.String;
import java.lang.String;
import java.util.List;

/**
 * 用户
 *
 * @author xhy
 **/
public class UserDaoImpl implements UserDaoCustom {
    private static Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
    @PersistenceContext
    private EntityManager em;

    @Override
    public List<User> find(UserDTO userDTO, Boolean pageFlag, String sort, String order) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
        Root<User> root = criteriaQuery.from(User.class);
        criteriaQuery.select(root);
        Predicate where = genWhereCondition(userDTO, root, criteriaBuilder);
        if (where != null) {
            criteriaQuery.where(where);
        }
        //排序
        TypedQuery<User> tq = em.createQuery(criteriaQuery);
        if (pageFlag) {
            PageRequest pagerequest = PageRequest.of(userDTO.getOffset() / userDTO.getLimit(), userDTO.getLimit());
            tq.setFirstResult(pagerequest.getPageNumber() * pagerequest.getPageSize()).setMaxResults(pagerequest.getPageSize());
        }
        return tq.getResultList();
    }

    @Override
    public long count(UserDTO userDTO) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
        Root<User> root = criteriaQuery.from(User.class);
        criteriaQuery.select(criteriaBuilder.count(root));
        Predicate where = genWhereCondition(userDTO, root, criteriaBuilder);
        if (where != null) {
            criteriaQuery.where(where);
        }
        return em.createQuery(criteriaQuery).getSingleResult();
    }

    private Predicate genWhereCondition(UserDTO userDTO, Root<User> root, CriteriaBuilder
            criteriaBuilder) {
        Predicate where = null;
        String userCodeSearch = userDTO.getUserCode();
        if (StringUtils.isNotBlank(userCodeSearch)) {
            Path<String> userCodeSearchPath = root.get(User_.userCode);
            if (where != null) {
                where = criteriaBuilder.and(where, criteriaBuilder.like(userCodeSearchPath, "%" + userCodeSearch + "%"));
            } else {
                where = criteriaBuilder.like(userCodeSearchPath, "%" + userCodeSearch + "%");
            }
        }
        String phoneSearch = userDTO.getPhone();
        if (StringUtils.isNotBlank(phoneSearch)) {
            Path<String> phoneSearchPath = root.get(User_.phone);
            if (where != null) {
                where = criteriaBuilder.and(where, criteriaBuilder.like(phoneSearchPath, "%" + phoneSearch + "%"));
            } else {
                where = criteriaBuilder.like(phoneSearchPath, "%" + phoneSearch + "%");
            }
        }
        return where;
    }
}
